//    Copyright (C) Kherty.  All rights reserved.
using System.Collections.ObjectModel;
using OpenLS.Core.Serialization;

namespace OpenLS.Spreadsheet
{
    internal class ColorScale: IOfficeXmlSerializable
    {
        internal ColorScale()
        {
            
        }


        #region IOfficeXmlSerializable Members

        private const string xmlName = "colorScale";
        void IOfficeXmlSerializable.ReadXml(ReadContext context)
        {
            using (ReadContext c = context.Read(xmlName))
            {
                while (c.ReaderLocalName == "cfvo")
                    Conditions.Add(c.ReadElement<ConditionalFormatValueObject>());
                Colors.Add(SpreadsheetColor.ReadColor("color", c));
                while (c.ReaderLocalName == "color")
                {
                    Colors.Add(SpreadsheetColor.ReadColor("color", c));
                }
            }
        }


        void IOfficeXmlSerializable.WriteXml(WriteContext context)
        {
            using (WriteContext c = context.Write(xmlName))
            {
                foreach (var condition in Conditions)
                {
                    c.WriteElement(condition);
                }
                foreach (SpreadsheetColor color in Colors)
                {
                    color.WriteXmlAs("color", c);
                }
            }
        }

        #endregion

        readonly ObservableCollection<ConditionalFormatValueObject> _conditions = new ObservableCollection<ConditionalFormatValueObject>();
        public ObservableCollection<ConditionalFormatValueObject> Conditions { get { return _conditions; } }
        readonly ObservableCollection<SpreadsheetColor> _colors = new ObservableCollection<SpreadsheetColor>();

        private ColorScale(ObservableCollection<ConditionalFormatValueObject> conditions, ObservableCollection<SpreadsheetColor> colors)
        {
            _conditions = conditions;
            _colors = colors;
        }

        public ObservableCollection<SpreadsheetColor> Colors { get { return _colors; } }

        public ColorScale Clone()
        {
            var result = new ColorScale(Conditions, Colors);
            return result;
        }
    }
}
